package com.itheima.service.impl;

/**
 * @author Dugen.Zhu
 * @date 2019-12-29 11:11
 */

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 预约设置服务
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                //调用dao查询该日期是否存在
                int count = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
                if (count == 0) {
                    //不存在，执行添加操作
                    orderSettingDao.add(orderSetting);
                } else {
                    //存在，执行更新操作
                    orderSettingDao.update(orderSetting);
                }
            }
        }
    }

    @Override
    public List<OrderSetting> findOrderSettingByMonth(String data) {
        //如果日期格式是2019-10
        if (data.length() == 7) {
            return orderSettingDao.findOrderSettingByMonth(data);
        } else {
            //日期格式是2019-9
            StringBuilder sb = new StringBuilder(data);
            sb.insert(5,"0");
            return orderSettingDao.findOrderSettingByMonth(sb.toString());
        }
    }

    @Override
    public void setNumberByDate(OrderSetting orderSetting) {
        orderSettingDao.setNumberByDate(orderSetting);
    }
}
